package cn.ting.mybatis.session.impl;

import cn.ting.mybatis.cache.Cache;
import cn.ting.mybatis.cache.CacheWapper;
import cn.ting.mybatis.executor.Executor;
import cn.ting.mybatis.executor.MyExecutor;
import cn.ting.mybatis.io.XMLConfigBuilder;
import cn.ting.mybatis.po.Configuration;
import cn.ting.mybatis.po.User;
import cn.ting.mybatis.session.SqlSession;
import cn.ting.mybatis.session.SqlSessionFactory;

import java.io.InputStream;

/**
 * 〈功能简述〉<br> 
 * 〈〉
 *
 * @author ting
 *

 */
public class MySqlSessionFactory implements SqlSessionFactory {
    private InputStream is;
    public void setIs(InputStream is) {
        this.is = is;
    }

    /**
     * 打开MySqlSession
     * */
    public SqlSession openSession() {
        MySqlSession sqlSession = new MySqlSession();
        //解析xml
        Configuration config = XMLConfigBuilder.loadXmlConfiguration(is);
        //打开数据源
        //创建执行器，进行增删改查，把配置文件信息传进去，因为配置文件信息里有数据源的连接信息
         Executor executor=new MyExecutor(config);
         User user=new User();
         sqlSession.setUser(user);
         sqlSession.setExecutor(executor);
         sqlSession.setConfiguration(config);

         //为executor或者说sqlsession创建缓存
        CacheWapper.putInCachePool(executor);
        return sqlSession;
    }
}
